package com.ssm.blog.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.blog.pojo.Photo;
import com.ssm.blog.service.PhotoService;
import com.ssm.blog.util.FilePOJO;
import com.ssm.blog.util.Funcs;
import com.ssm.blog.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/photo.html")
public class PhotoController {

    @Autowired
    private PhotoService photoService;
    @Value("${PHOTO_IMAGE_DIR}")
    private String PHOTO_IMAGE_DIR;
    @Value("${PHOTO_IMAGE_URL}")
    private String PHOTO_IMAGE_URL;

    @RequestMapping(params = "act=page")
    public ModelAndView page() {
        return new ModelAndView("photo_list", "PHOTO_IMAGE_URL", PHOTO_IMAGE_URL);
    }

    @RequestMapping(params = "act=upload")
    @ResponseBody
    public Map<String, Object> uploadPhoto(@RequestParam("file") MultipartFile file, HttpSession session) {
        List<FilePOJO> fileList = new ArrayList<>();
        try {
            String newFileName = Funcs.uploadFile(file, PHOTO_IMAGE_DIR);
            //添加到数据库
            fileList.add(new FilePOJO(newFileName, file.getSize(), PHOTO_IMAGE_URL + newFileName, PHOTO_IMAGE_URL + newFileName, null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("files", fileList);//这个key也是固定的
        return result;
    }

    @RequestMapping(params = "act=upload2")
    @ResponseBody
    public String upload(MultipartFile file) {
        try {
            String newFileName = Funcs.uploadFile(file, PHOTO_IMAGE_DIR);
            return "{\"error\":0,\"url\":\"" + PHOTO_IMAGE_URL + newFileName + "\"}";
        } catch (IOException e) {
            e.printStackTrace();
            return "{\"error\":1}";
        }
    }

    @RequestMapping(params = "act=add")
    @ResponseBody
    public Map<String, Object> addPhoto(String[] fileNames, HttpSession session) {
        for (String fileName : fileNames) {
            photoService.addPhoto(new Photo(fileName, 1));
        }
        return JsonUtil.getSuccessMessage("上传成功", null);
    }

    @RequestMapping(params = "act=list")//使用ajax进行分页查询
    @ResponseBody
    public Map<String, Object> getPhotoList(Integer page, Integer rows) {
        PageInfo<Photo> pageInfo = photoService.getPhotoList(page, rows);
        return JsonUtil.getDatagridPagerResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(Integer id) {
        photoService.deletePhotoById(id);
        return JsonUtil.getSuccessMessage("删除成功", null);
    }
}
